#include "TestState.h"
#include "DX11ErrorReport.h"
#include "DX11HelpfulFunctions.h"
#include "Timer.h"
#include "CameraManager.h"
#include "LightingManager.h"
#include "Vec3.h"
#include "Game.h"
#include "AntTweakBarManager.h"
#include "DX11TextureManager.h"
#include "DX11ShaderManager.h"
#include "Text.h"
#include "ConfigManager.h"
#include "Get3DMousePos.h"
#include "OptionsState.h"

#include "BPlay.h"
#include "BSVolume.h"
#include "BLoadSound.h"
#include "BSaveSound.h"
#include "BPlayOnChange.h"
#include "BSLPFilter.h"
#include "BSMaxFreq.h"
#include "BSMinFreq.h"
#include "BSHPFilter.h"
#include "BSFrequency.h"
#include "BReverse.h"
#include "BNormalised.h"
#include "BSampleSound.h"
#include "InfoState.h"
#include "Sound.h"

TestState::TestState()
{
	m_rotCrateAngle = 0;
	for (int i = 0 ; i != 15; i++)
	{
		axis[i].x = 0;
		axis[i].y = 0;
		axis[i].z = 0;
	}

	m_sampleSounds.SetFont("pixel_small", L"../../Assets/pixel2.png");
	m_sampleSounds.Translate(D3DXVECTOR3(0.0f, 0.1f, 0.0f));
	m_sampleSounds.Update(" ------Sample Sounds------");

	
	m_soundModifiers.SetFont("pixel_small", L"../../Assets/pixel2.png");
	m_soundModifiers.Translate(D3DXVECTOR3(0.0f, 0.9f, 0.0f));
	m_soundModifiers.Update(" ------Sound Modifiers------");
			
	m_controllers.SetFont("pixel_small", L"../../Assets/pixel2.png");
	m_controllers.Translate(D3DXVECTOR3(0.0f, -0.6f, 0.0f));
	m_controllers.Update(" ------Controllers------");

	m_showOverlay = true;
}

TestState::~TestState()
{
}

void TestState::Draw()
{
	DX11::ClearColour(0.5f, 0.5f, 0.5f, 1.0f);
	
	//TheGame::Instance()->DrawColourPickObjects();
	
	
	TheScreenManager::Instance()->GetDeviceContext()->OMSetRenderTargets(1, 
		TheScreenManager::Instance()->GetRenderTargetview(),
		TheScreenManager::Instance()->GetDepthStencilView());		
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);
	TheCameraManager::Instance()->Update();
	
	DX11::ClearColour(0.6039f, 0.5960f, 1.0f, 1.0f);

	TheCameraManager::Instance()->Draw();
	TheGame::Instance()->DrawGameObjects();
	
	static bool b = m_sound.LoadSound("../../Assets/boss.wav");

	D3DXVECTOR2 pos = D3DXVECTOR2(0.2f, 0.7f);
	static float increVal = 0.15f;

	m_soundModifiers.Draw();
	m_controllers.Draw();

	static BSVolume* bs = new BSVolume();
	bs->SetPos(pos);
	bs->SetSound(&m_sound);
	bs->Update();
	bs->Draw();
	pos.y -= increVal;
	
	static BSLPFilter lp;
	lp.SetPos(pos);
	lp.SetSound(&m_sound);
	lp.Update();
	lp.Draw();
	pos.y -= increVal;

	static BSFrequency freq;
	freq.SetPos(pos);
	freq.SetSound(&m_sound);
	freq.Update();
	freq.Draw();
	pos.y -= increVal;

	static BPlay play;
	play.SetSound(&m_sound);
	play.Update();
	play.Draw();

	static BPlayOnChange playOnChange;
	playOnChange.SetSound(&m_sound);
	playOnChange.Update();
	playOnChange.Draw();

	static BLoadSound ls;
	ls.Update();
	ls.Draw();

	static BSaveSound ss;
	ss.Update();
	ss.Draw();

	static BReverse br;
	br.SetSound(&m_sound);
	br.Update();
	br.Draw();

	static BNormalised bn;
	bn.SetSound(&m_sound);
	bn.Update();
	bn.Draw();

	// sample wavs
	m_sampleSounds.Draw();

	// top row
	static BSampleSound m_sampDing;
	m_sampDing.SetSound(&m_sound);
	m_sampDing.SetFile("../../Assets/Ding.wav");
	m_sampDing.SetName(" Ding");
	m_sampDing.SetPos(D3DXVECTOR3(-0.6f, -0.1f, 0.0f));
	m_sampDing.Update();
	m_sampDing.Draw();

	static BSampleSound m_sampBoss;
	m_sampBoss.SetSound(&m_sound);
	m_sampBoss.SetFile("../../Assets/boss.wav");
	m_sampBoss.SetName(" Boss Spawn");
	m_sampBoss.SetPos(D3DXVECTOR3(0.0f, -0.1f, 0.0f));
	m_sampBoss.Update();
	m_sampBoss.Draw();
	
	static BSampleSound m_sampPistol;
	m_sampPistol.SetSound(&m_sound);
	m_sampPistol.SetFile("../../Assets/pistol.wav");
	m_sampPistol.SetName(" Plasma Pistol");
	m_sampPistol.SetPos(D3DXVECTOR3(0.6f, -0.1f, 0.0f));
	m_sampPistol.Update();
	m_sampPistol.Draw();
	
	// bottom row
	static BSampleSound m_sampTele;
	m_sampTele.SetSound(&m_sound);
	m_sampTele.SetFile("../../Assets/teleport.wav");
	m_sampTele.SetName(" Teleport");
	m_sampTele.SetPos(D3DXVECTOR3(-0.6f, -0.2f, 0.0f));
	m_sampTele.Update();
	m_sampTele.Draw();

	static BSampleSound m_sampExplosion;
	m_sampExplosion.SetSound(&m_sound);
	m_sampExplosion.SetFile("../../Assets/explosion.wav");
	m_sampExplosion.SetName(" Explosion");
	m_sampExplosion.SetPos(D3DXVECTOR3(0.0f, -0.2f, 0.0f));
	m_sampExplosion.Update();
	m_sampExplosion.Draw();
	
	static BSampleSound m_sampNoise;
	m_sampNoise.SetSound(&m_sound);
	m_sampNoise.SetFile("../../Assets/noise.wav");
	m_sampNoise.SetName(" Noise");
	m_sampNoise.SetPos(D3DXVECTOR3(0.6f, -0.2f, 0.0f));
	m_sampNoise.Update();
	m_sampNoise.Draw();
	

	TheDX11ShaderManager::Instance()->ResetResources();
}

void TestState::Update()
{
	TheCameraManager::Instance()->Update();
	TheGame::Instance()->UpdateGameObjects();

	if (m_showOverlay)
	{
		TheGame::Instance()->SetGameState(TheInfoState::Instance());
	}
}

void TestState::SetSound(const std::string& s)
{
	m_sound.LoadSound(s);
}

void TestState::SaveSound(const std::string& s)
{
	m_sound.SaveSound(s);
}

void TestState::OnKeyDownEvent(const WPARAM& event)
{
	if (event == VK_RETURN)
	{
		m_showOverlay = true;
	}
	else if (event == VK_ESCAPE)
	{
		TheGame::Instance()->SetGameState(TheOptionsState::Instance());
	}
}

void TestState::OnMouseMotionEvent(const LPARAM& event)
{
	int x = LOWORD(event);
	int y = HIWORD(event);
	
	int screenWidth = TheScreenManager::Instance()->GetWindowWidth();
	int screenHeight = TheScreenManager::Instance()->GetWindowHeight();
	
	if (x <= screenWidth && x >= 0)
	{
		m_mouseX = (float)x;
	}
	if (y <= screenHeight && y >= 0)
	{
		m_mouseY = (float)y;
	}
}
void TestState::OnMouseButtonDownEvent(const MSG& event)
{
	m_mouseDown = true;
}

void TestState::OnMouseButtonUpEvent(const MSG& event)
{
	m_mouseDown = false;
}

void TestState::OnActivated()
{
	m_showOverlay = false;
	TheEventPoller::Instance()->AddHandler(this);
}

void TestState::OnDeactivated()
{
	m_showOverlay = false;
	TheEventPoller::Instance()->RemoveHandler(this);
}

void TestState::GetAxisArray(Vec3f axes[])
{
	for (int i = 0; i != 15; i++)
	{
		axis[i] = axes[i];
	}
}
